package com.pl;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.nio.channels.Channels;
import java.nio.channels.DatagramChannel;
import java.util.HashMap;

public class ReferenceTest {
    public static void main(String[] args) {
        //强引用：不管怎么样都不会被回收，一直处于可达状态
        Object strongReference = new Object();
        //软引用：当内存足够高时不回收，内存不足时才会回收
        SoftReference<Object> softReference = new SoftReference<>(new HashMap<String, String>());
        //弱引用：只要触发GC必被回收
        WeakReference<Object> weakReference = new WeakReference<>(new Object());
        //虚引用：跟踪对象被垃圾回收的状态，需要引用队列一起使用
        PhantomReference<Object> phantomReference = new PhantomReference<>(new Object(), new ReferenceQueue<>());

        //使用：
        Object soft = softReference.get();
        Object weak = weakReference.get();
        Object phantom = phantomReference.get();


        Channels.newChannel(new InputStream() {
            @Override
            public int read() throws IOException {
                return 0;
            }
        });

        Channels.newChannel(new OutputStream() {
            @Override
            public void write(int b) throws IOException {

            }
        });

        DatagramChannel datagramChannel;
    }
}
